package com.kzj.fun.container.root;

import com.kzj.common.log.Logger;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class RootFrame extends JFrame implements  NativeKeyListener {

    final private GraphicsDevice graphicsDevice;
    final private int exitButton = NativeKeyEvent.VC_0;
    private volatile RootPanel content;
    private final Robot robot;

    public RootFrame(){
        try {
            robot=new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
        this.setType(Type.UTILITY);
        // 获取默认图形设备
        graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        this.setTitle("全屏鼠标跟随方框");
        this.setUndecorated(true); // 去掉窗口边框
        this.setExtendedState(JFrame.MAXIMIZED_BOTH); // 全屏显示
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 获取屏幕尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize.width, screenSize.height);
        this.setLocation(0, 0); // 左上角对齐到屏幕的左上角
        //把屏幕模式改为独占模式
        this.graphicsDevice.setFullScreenWindow(this);
        // 强制获取焦点，确保事件可以被监听
        this.setFocusable(true);
        //自定义鼠标光标
        this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
//        this.addWindowStateListener(this);
        this.setState(Frame.ICONIFIED);

        // 注册全局快捷键
//        registerGlobalHotkey();

        this.setVisible(true);
    }
    public void iconified(){
        Logger.debug("最大化-> 最小化");
        setState(Frame.ICONIFIED);
        content.iconified();
    }
    public void normal(){
        Logger.debug("最小化-> 最大化");
        BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        try {
            ImageIO.write(screenCapture,"png",new File("C:\\Users\\KZJ\\Desktop\\rss\\app\\test.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if(content!=null){
            content.normal(screenCapture);
        }else {
            content = new RootPanel(screenCapture,this);
            setContentPane(content);
        }
        revalidate();
        setState(Frame.NORMAL);
    }
    private boolean altDown=false;
    private boolean ctrlDown=false;
    @Override
    public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {

    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        if(e.getKeyCode()==NativeKeyEvent.VC_ALT)
        {
            altDown=true;
        }
        if(e.getKeyCode()==NativeKeyEvent.VC_CONTROL){
            ctrlDown=true;
        }

        if(e.getKeyCode()==NativeKeyEvent.VC_F2){
            if((getExtendedState()&Frame.ICONIFIED)!=0){
                normal();
            }else{
                iconified();
            }
        }
        if((getState()&Frame.ICONIFIED)==0){
            Logger.debug("键盘点击 -> "+e.getKeyCode());
            if(e.getKeyCode()==NativeKeyEvent.VC_TAB&&altDown){
                iconified();
            }else if(e.getKeyCode()==NativeKeyEvent.VC_C&&ctrlDown){
                Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                Point location = MouseInfo.getPointerInfo().getLocation();
                int rgb = content.imageHistory.peek().getRGB(location.x, location.y);
                // 解析 ARGB 值
                int alpha = (rgb >> 24) & 0xFF; // Alpha
                int red = (rgb >> 16) & 0xFF;   // Red
                int green = (rgb >> 8) & 0xFF;  // Green
                int blue = (rgb) & 0xFF;        // Blue
                String rgbText=String.format("%d , %d , %d",red,green,blue);
                StringSelection stringSelection=new StringSelection(rgbText);
                systemClipboard.setContents(stringSelection,null);
            }else{
                Point location = MouseInfo.getPointerInfo().getLocation();
                switch (e.getKeyCode()){
                    case exitButton:{
                        iconified();
                    }break;
                    case NativeKeyEvent.VC_UP:{
                        robot.mouseMove(location.x,location.y-1);
                    }break;
                    case NativeKeyEvent.VC_DOWN:{
                        robot.mouseMove(location.x,location.y+1);
                    }break;
                    case NativeKeyEvent.VC_LEFT:{
                        robot.mouseMove(location.x-1,location.y);
                    }break;
                    case NativeKeyEvent.VC_RIGHT:{
                        robot.mouseMove(location.x+1,location.y);
                    }break;
                }
            }
        }
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        if (e.getKeyCode() == NativeKeyEvent.VC_ALT) {
            altDown = false;
        }
        if(e.getKeyCode()==NativeKeyEvent.VC_CONTROL){
            ctrlDown = false;
        }
    }
}
